package work.igames.vertxcreate.service.impl;

import io.vertx.core.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import work.igames.vertxcreate.annotation.Parameter;
import work.igames.vertxcreate.dao.UserSkillMapper;
import work.igames.vertxcreate.enitity.UserSkill;
import work.igames.vertxcreate.listen.ApplicationLunch;
import work.igames.vertxcreate.service.UserSkillAsyncService;

import java.util.List;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicReference;

/**
 * @ClassName UserSkillAsyncServiceImpl
 * @Description
 * @Author Jingxiao.Ye
 * @Date 2020/10/30 上午11:37
 * @Version V1.0
 * @Since JDK 11
 */
@Service
public class UserSkillAsyncServiceImpl implements UserSkillAsyncService {

    @Autowired
    private UserSkillMapper userSkillMapper;

    @Override
    public void findAll(Handler<AsyncResult<List<UserSkill>>> handler) {
        AtomicReference<List<UserSkill>> userSkillList1 = new AtomicReference<>();
        ApplicationLunch.vertx.executeBlocking((promise) -> {
            List<UserSkill> userSkillList = userSkillMapper.selectAll();
            promise.complete(userSkillList);
        }, (AsyncResult<List<UserSkill>> result) -> {
            userSkillList1.set(result.result());
            handler.handle(result);
        });
    }

    @Override
    public void findByName(@Parameter String name, Handler<AsyncResult<UserSkill>> handler) {
        ApplicationLunch.vertx.executeBlocking((promise) -> {
            Example exampleUserSkill = new Example(UserSkill.class);
            exampleUserSkill.createCriteria().andEqualTo("name", name);
            UserSkill userSkill = userSkillMapper.selectOneByExample(exampleUserSkill);
            promise.complete(userSkill);
        }, handler);

    }

}
